package forminrecipe.core;

import forminrecipe.data.*;
import forminrecipe.GUI.GUI;

public class Core {
	private static GUI gui;
	private static dbManager dbm;
	private static SQLSettings sqlSettings;
	private static FileIO fileIO;
	
	//constructor
	public Core(){
		//read in settings file
		fileIO = new FileIO();
		sqlSettings = fileIO.ReadSettingsFile();
		dbm=new dbManager(this);
		gui = new GUI(this, sqlSettings);	
	}
	
	public boolean start(){
		System.out.println("Core: Starting up ....");
		
		//connect to database
		if (dbm.connectToDatabase(sqlSettings)){
			System.out.println("Core: Successfully connected to mySQLServer:");	
		}else{
			System.out.println("Core: System failed to connect to mySQLServer:");
			return false;
		}
		
		//read catagories database
		if (dbm.readCatagoryDataBase()){
			System.out.println("Core: Catagories information successfully retreived from mySQLServer:");
		} else {
			System.out.println("Core: Error Reading Catagories from mySQLServer:");
			return false;
		}
		
		//read list database
		if (dbm.readListDatabase()){
			System.out.println("Core: List information successfully retreived from mySQLServer:");
		}else{
			System.out.println("Core: Error Reading List from mySQLServer:");
			return false;
		}
		
		// read recipelist database
		if(dbm.readRecipeListDatabase()){
			System.out.println("Core: RecipeList information successfully retreived from mySQLServer:");
		}else{
			System.out.println("Core: Error Reading RecipeList from mySQLServer:");
			return false;
		}
		
		//gui.setSelectedRecipe(0);
		gui.refreshGUI();
		gui.setVisible(true);
		return true;
	}
	
	public void addCatagoryGUI(String catagory){
		gui.addCatagoryGUI(catagory);
	}
	public void addCatagoryDBM(String catagory){
		dbm.addCatagoryDBM(catagory);
	}
	public void addListGUI(String item){
		gui.addListGUI(item);
	}
	public void addRecipeListGUI(RecipeData recipeData){
		gui.addRecipeListGUI(recipeData);
	}
	public void addListItemDBM(String item){
		dbm.addListItemDBM(item);
	}
	public void saveRecipeDBM(String name, int rating, int difficulty, String time, String catagory, String process ){
		dbm.saveRecipeDBM(name,rating,difficulty,time,catagory,process);
	}
	public void getIngredientsForDMB(int id){
		dbm.getIngredientsForDMB(id);
	}

	public void saveSQLSettings(SQLSettings sqlSettings){
		FileIO.saveSQLSettings(sqlSettings);	
	}
	public void addIngredientGUI(String quantity, String units, String item){
		gui.addIngredientGUI(quantity, units, item);
	}

	public static void main(String[] args) {
		Core core = new Core();
		core.start();
	}
}	

 